package com.kimo.shop.admin.controller;

import com.kimo.shop.bean.model.AttachFileGroup;
import com.kimo.shop.service.AttachFileGroupService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author kimo-taozhouchuan
 * @date 2021/7/7 17:18
 */
@RestController
@RequestMapping("/admin/fileGroup")
public class AttachFileGroupController {

    @Autowired
    private AttachFileGroupService attachFileGroupService;

    @GetMapping("/list")
    @ApiOperation(value = "获取列表", notes = "分页获取列表")
    public ResponseEntity<List<AttachFileGroup>> list(@RequestParam(value = "type", defaultValue = "1") Integer type) {
        List<AttachFileGroup> attachFileGroupPage = attachFileGroupService.list(type);
        return ResponseEntity.ok(attachFileGroupPage);
    }

    @GetMapping
    @ApiOperation(value = "获取", notes = "根据attachFileGroupId获取")
    public ResponseEntity<AttachFileGroup> getByAttachFileGroupId(@RequestParam Long attachFileGroupId) {
        return ResponseEntity.ok(attachFileGroupService.getByAttachFileGroupId(attachFileGroupId));
    }

    @PostMapping
    @ApiOperation(value = "保存", notes = "保存")
    public ResponseEntity<Void> save(@Valid @RequestBody AttachFileGroup attachFileGroup) {
        attachFileGroup.setAttachFileGroupId(null);
        attachFileGroupService.saveGroup(attachFileGroup);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    @ApiOperation(value = "更新", notes = "更新")
    public ResponseEntity<Void> update(@Valid @RequestBody AttachFileGroup attachFileGroup) {
        attachFileGroupService.update(attachFileGroup);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping
    @ApiOperation(value = "删除", notes = "根据id删除")
    public ResponseEntity<Void> delete(@RequestParam Long attachFileGroupId) {
        AttachFileGroup dbAttachFileGroup = attachFileGroupService.getByAttachFileGroupId(attachFileGroupId);
        attachFileGroupService.deleteById(attachFileGroupId);
        return ResponseEntity.ok().build();
    }
}
